package com.coding.filter;

import com.alibaba.fastjson2.JSON;
import com.coding.dto.RestBean;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author Cynicism
 * @version 1.0
 * @project DyApp
 * @description 统一鉴权
 * @date 2024/7/1 16:14:15
 */
@Component
public class AuthFilter implements GlobalFilter {

    private String[] whiteList = {"/user/test", "/user/reg","/code","/phonecode","/sendmailCode",
            "/user/login",
            "/user/loginAndCode",
            "/user/emailAndpwd",
            "/user/mailAndcode",
            "/user/Changepassword",
            "v5/userList/queryAllFollower",
            "/live/AllLiveInfo"};

    @Resource
    private RedisTemplate<String, Object> MyRedisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        /**
         * 前端：判断要跳转的路由有没有在白名单内，如果有直接放行，如果没有那就判断有没有携带token
         * 后端：直接获取请求头里面的X-Token，通过配置类去放行某些接口
         */
        ServerHttpRequest request = exchange.getRequest();
        PathContainer pathContainer = request.getPath().pathWithinApplication();
        System.out.println("pathContainer:" + pathContainer);///user/test
        System.out.println("URI:" + request.getURI().getPath());///user/test
        return chain.filter(exchange);
//
//        for (String s : whiteList) {
//            if (s.equals(pathContainer.value())) {
//                return chain.filter(exchange);
//            }
//        }
//        if (request.getHeaders().containsKey("X-Token")){
//            String token = request.getHeaders().getFirst("X-Token");
//
////            if (token != null && Boolean.TRUE.equals(MyRedisTemplate.hasKey(token))) {
////                Object object = MyRedisTemplate.opsForValue().get(token);
////                System.out.println(object);
////                System.out.println("鉴权成功");
////                return chain.filter(exchange);
////            }
//        }
////        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//        ServerHttpResponse response = exchange.getResponse();
//        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
//        RestBean<String> restBean = RestBean.unauthorized("鉴权失败2");
//        DataBuffer wrap = response.bufferFactory().wrap(JSON.toJSONBytes(restBean));
//        return response.writeWith(Mono.just(wrap));
    }
}
